package com.wk.kxexamadminister.controller;

import com.wk.kxexamadminister.entity.Teacher;
import com.wk.kxexamadminister.mapper.TeacherMapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(description = "教师controller")
@Controller
public class TeacherController {

    @Autowired
    private TeacherMapper teacherMapper;

    @PostMapping("/addTeacher")
    public String addTeacher(Teacher teacher){
        int insert = teacherMapper.insert(teacher);
        if(insert == 1) {
            return "redirect:/findTeacher";
        }
        return "error";
    }

    @GetMapping("/delTeacher")
    @ResponseBody
    public String delTeacher(Integer tId){
        int i = teacherMapper.deleteById(tId);
        if(i == 1) {
            return "Success";
        }
        return "No";
    }

    @GetMapping("/updTeacher")
    public String updTeacher(Teacher teacher){
        int i = teacherMapper.updateById(teacher);
        if(i == 1) {
            return "redirect:/findTeacher";
        }
        return "error";
    }

    @GetMapping("/findTeacher")
    public String findTeacher(Model model){
        List<Teacher> teachers = teacherMapper.selectList(null);
        model.addAttribute("allTeacher",teachers);
        return "07-queryAllTeachers";
    }









}
